package com.edvard.util;

import java.sql.SQLException;

import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.util.Log;

import com.edvard.bean.FiancialData;
import com.edvard.bean.FirstSubject;
import com.edvard.bean.History;
import com.edvard.bean.SecondSubject;
import com.edvard.bean.Summery;
import com.j256.ormlite.android.apptools.OrmLiteSqliteOpenHelper;
import com.j256.ormlite.dao.Dao;
import com.j256.ormlite.support.ConnectionSource;
import com.j256.ormlite.table.TableUtils;

public class DatabaseHelper extends OrmLiteSqliteOpenHelper {
	
	//创建的数据名的表明
	private static final String DATABASE_NAME = "financial.db";
	//数据库的版本号
	private static final int DATABASE_VERSION = 1;

	//对应映射对象的dao对象
	private static Dao<FirstSubject, Integer> firstSubject = null;
	private static Dao<SecondSubject, Integer> secondSubject = null;
	private static Dao<Summery, Integer> summery = null;
	private static Dao<History, Integer> history = null;
	private static Dao<FiancialData, Integer> fiancialData = null;

	public DatabaseHelper(Context context) {
		// TODO Auto-generated constructor stub
		super(context, DATABASE_NAME, null, DATABASE_VERSION);
	}

	/**
	 * 当程序第一次启动的时候会调用的方法
	 * 主要使用创建数据库
	 */
	@Override
	public void onCreate(SQLiteDatabase sqliteDatabase,
			ConnectionSource connectionSource) {
		// TODO Auto-generated method stub
		try {
			TableUtils.createTable(connectionSource, History.class);
			TableUtils.createTable(connectionSource, Summery.class);
			TableUtils.createTable(connectionSource, FirstSubject.class);
			TableUtils.createTable(connectionSource, SecondSubject.class);
			TableUtils.createTable(connectionSource, FiancialData.class);
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			Log.e(DatabaseHelper.class.getName(), "Unable to create datbases",
					e);
			e.printStackTrace();
		}
	}
	
	/**
	 * 当数据库需要更新的时候会调用的方法
	 * 用于数据库结构的改动等操作
	 */
	@Override
	public void onUpgrade(SQLiteDatabase sqLiteDatabase,
			ConnectionSource connectionSource, int oldVer, int newVer) {
		// TODO Auto-generated method stub

	}

	/**
	 * 得到bean对象的
	 * 
	 * @return
	 * @throws SQLException
	 */

	public Dao<FirstSubject, Integer> getFirstSubject() throws SQLException {
		if (firstSubject == null) {
			firstSubject = getDao(FirstSubject.class);
		}
		return firstSubject;
	}

	public Dao<SecondSubject, Integer> getSecondSubject() throws SQLException {
		if (secondSubject == null)
			secondSubject = getDao(SecondSubject.class);
		return secondSubject;
	}

	public Dao<Summery, Integer> getSummery() throws SQLException {
		if (summery == null)
			summery = getDao(Summery.class);
		return summery;
	}

	public Dao<History, Integer> getHistory() throws SQLException {
		if (history == null)
			history = getDao(History.class);
		return history;
	}

	public Dao<FiancialData, Integer> getFiancialData() throws SQLException {
		if (fiancialData == null)
			fiancialData = getDao(FiancialData.class);
		return fiancialData;
	}

}
